package com.barbeque.mall.service.admin.comment.impl;

import com.barbeque.mall.bean.comment.MarketComment;
import com.barbeque.mall.bean.comment.MarketCommentExample;
import com.barbeque.mall.bean.comment.bo.CommentListBO;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketCommentMapper;
import com.barbeque.mall.service.admin.comment.AdminCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Euphonium
 * @since 2022/07/19
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public CommonData<MarketComment> listComments(CommentListBO commentListBO) {
        PageHelper.startPage(commentListBO.getPage(), commentListBO.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        // 根据用户ID查询
        if (commentListBO.getUserId() != null) {
            criteria.andUserIdEqualTo(commentListBO.getUserId());
        }
        // 根据商品ID查询
        if (commentListBO.getValueId() != null) {
            criteria.andValueIdEqualTo(commentListBO.getValueId());
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(commentListBO.getSort() + " " + commentListBO.getOrder());
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);
        return CommonData.data(pageInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteComment(MarketComment marketComment) {
        marketComment.setDeleted(true);
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
    }
}
